#!/usr/bin/python
# -*- coding: UTF-8 -*-
from selenium import webdriver;
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re;
from pyquery import PyQuery as pq;
from lxml import etree
import pymongo;
from  config import *
# browser=webdriver.PhantomJS(service_args=SERVICE_ARGS);
client = pymongo.MongoClient(MONGO_URL);
db = client["xiecheng"];
TABLE="holtel"
from bs4 import BeautifulSoup
import time
import requests
import pandas as pd
import requests
from utils.Yundama import YDMHttp

browser = webdriver.Chrome()
# browser.set_window_size(1400,900)
wait = WebDriverWait(browser, 10);


def search():
    url = "http://hotels.ctrip.com/hotel/hangzhou17#ctm_ref=hod_hp_sb_lst"
    browser.get(url)
    pages = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#page_info > div.c_page_list.layoutfix > a:nth-child(9)"))
    )
    get_hotel()
    return pages.text


#点击隐藏按钮
def clickHideButton(driver, element):
    driver.execute_script("arguments[0].click()", element);

def saveToMongo(result):
    try:
        if db[TABLE].insert(result):
            print("save result success");
    except:
        print("save mongo error")

def get_hotel():
    pages = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#hotel_list .hotel_new_list"))
    )
    html=browser.page_source
    # print(html)
    soup = BeautifulSoup(html, 'lxml')
    items=soup.select(".hotel_new_list")
    for item in items:
        try:
            tags_list=[]
            tags=item.select(".special_label i")
            for tag in tags:
                tags_list.append(tag.string)

            sight={
                "name":item.select(".hotel_name a")[0].attrs["title"],
                "url":"http://you.ctrip.com"+item.select(".hotel_name a")[0].attrs["href"],
                "score":item.select(".hotel_value")[0].string,
                "com_count":item.select(".hotel_judgement span")[0].string,
                "price":item.select(".J_price_lowList")[0].string,
                "address":item.select(".hotel_item_htladdress")[0].getText(),
                "tags":tags_list
             }
        except Exception as e:
            print(e)
            continue

        print(sight)
        saveToMongo(sight)



def next_page(page):
    print("currentpage", page)

    try:
        page_bu = browser.find_element_by_partial_link_text(u'下一页')
        browser.execute_script("arguments[0].scrollIntoView(false);", page_bu)

        next = wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        '#downHerf')))
        clickHideButton(browser, next)
        wait.until(
            EC.text_to_be_present_in_element(
                (By.CSS_SELECTOR,
                 '#page_info > div.c_page_list.layoutfix > a.current'),
                str(page)
            )
        )
    except TimeoutException as e:
        print(e)
        return

    get_hotel()


def main():
    pages=int(search())
    for page in range(261,pages+1):
        next_page(page)

if __name__ == '__main__':
    main()